#ifndef _COMMUNICATEMGR_H_
#define _COMMUNICATEMGR_H_

#include <iostream>
#include <cstring>

#include "sampleSys_def.h"

#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")

typedef struct _SOCKET_INFORMATION {
	char buffer[BUFFER_SIZE];
	WSABUF Databuf;
	SOCKET Socket;
	DWORD BytesSend;
	DWORD BytesRecv;
	int useTime;

}SOCKET_INFORMATION, * LPSOCKET_INFOMATION;

typedef struct _Port {
	SOCKET socketRecode;
	unsigned char devId[32];
}Port, *IPort;

class CommunicateMgr
{
public:
	~CommunicateMgr();

	static CommunicateMgr* getIt();

	void init();

	bool creatSocketInformation(SOCKET s);

	void freeSocketInformation(int Index);

	void creatPortInformation(LPSOCKET_INFOMATION s, unsigned char* devId);

	bool runSever();

private:
	CommunicateMgr();

private:
	static CommunicateMgr* s_head;

	LPSOCKET_INFOMATION SocketArray[FD_SETSIZE];
	
	Port PortsArray[PORT_MAX];

	SOCKET ListenSocket;
	SOCKET AcceptSocket;
	SOCKADDR_IN InternetAddr;
	WSADATA wsaData;

	FD_SET WriteSet;
	FD_SET ReadSet;

	DWORD SendBytes;
	DWORD RecvBytes;

	bool isRight;
	int Ret;
	int total;
	int TotalSockets;
	int TotalPorts;
	unsigned char packageType;
	
	ULONG NonBlock;
};

#endif // !_COMMUNICATEMGR_H_
